<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<div class="execution">
				<avue-crud
					ref="crud"
					v-model="form"
					:page="page"
					:data="tableData"
					:table-loading="tableLoading"
					:option="tableOption"
					:before-open="handleOpenBefore"
					@on-load="getList"
					@search-change="searchChange"
					@search-reset="searchreset"
					@refresh-change="refreshChange"
					@size-change="sizeChange"
					@current-change="currentChange"
					@row-update="handleUpdate"
					@row-save="handleSave"
					@row-del="rowDel"
				>
				</avue-crud>
			</div>
		</div>
	</div>
</template>

<script lang="ts" setup>

import { fetchList, addObj, putObj, delObj, getSupplierNumber } from '/@/api/basiciot/device/supplier';
import { tableOption } from '/@/crud/basiciot/device/group.js';
import { useMessage, useMessageBox } from '/@/hooks/message';


let tableLoading = ref(false)
let tableData = reactive([])
let form = reactive({})

let searchForm = reactive({
	supplierName: ''
})


let page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})


onMounted(() => {
})
const handleOpenBefore = (show:Function, type:string) =>{
		
		show();
	}

const getList = (page:any, params?:object) => {
			tableLoading.value = true;
			fetchList(
				Object.assign(
					{
						current: page.currentPage,
						size: page.pageSize,
					},
					params,
					searchForm
				)
			)
				.then((response) => {
					tableData = response.data.data.records;
					page.total = response.data.data.total;
					tableLoading.value = false;
				})
				.catch(() => {
					tableLoading.value = false;
				});
		}


		const rowDel =  (row:any, index: number) => {
			useMessageBox().confirm('是否确认删除ID为' + row.supplierId).then(() => {
				return delObj(row.supplierId).then(() => {
					useMessage().success('删除成功');
					getList(page);
				});
			});
		}
		const handleUpdate = (row:any, done: Function, loading:Function) => {
			putObj(row)
				.then((data) => {
					useMessage().success('修改成功');
					done();
					getList(page);
				})
				.catch(() => {
					loading();
				});
		}
		const handleSave = (row:any,  done: Function, loading:Function) =>{
			addObj(row)
				.then((data) => {
					useMessage().success('添加成功');
					done();
					getList(page);
				})
				.catch(() => {
					loading();
				});
		}
		const sizeChange = (pageSize:number) =>{
			page.pageSize = pageSize;
		}
		const currentChange = (current:number) => {
			page.currentPage = current;
		}
		const searchChange = (form:any, done:Function) =>{
			if (!searchForm.supplierName) {
				// delete searchForm.supplierName;
			}
			//searchForm = form
			page.currentPage = 1;
			getList(page, form);
			if (done) {
				done();
			}
		}
		const refreshChange = () =>{
			getList(page);
		}
		const handleRefreshChange = () => {
			getList(page);
		}
		const searchreset = () => {

		}
</script>


